package com.food.util;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author beixiang
 * @version 1.0
 * @description TODO
 * @date 2023/4/2 16:28
 */
@Slf4j
public class SecurityUtil {

    public static Users getUser() {
        try {
            Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principalObj instanceof String) {
                //取出用户身份信息
                String principal = principalObj.toString();
                //将json转成对象
                Users user = JSON.parseObject(principal, Users.class);
                return user;
            }
        } catch (Exception e) {
            log.error("获取当前登录用户身份出错:{}", e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    @Data
    public static class Users implements Serializable {


        /**
         * 用户名id
         */
        @TableId(type = IdType.AUTO)
        private Integer userId;

        /**
         * `user_id` int NOT NULL AUTO_INCREMENT COMMENT '用户id',
         *   `user_name` varchar(30) DEFAULT NULL COMMENT '用户账号',
         *   `user_password` varchar(30) DEFAULT NULL COMMENT '用户密码',
         *   `user_sex` varchar(10) DEFAULT NULL COMMENT '用户性别',
         *   `user_email` varchar(30) DEFAULT NULL COMMENT '用户邮箱',
         *   `user_reg_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '用户注册时间',
         *   `user_avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '/static/imag
         */
        /**
         * 用户账号
         */
        private String userName;
        /**
         * 用户性别
         */
        private String userSex;
        /**
         * 用户密码
         */
        private String userPassword;
        /**
         * 用户邮箱
         */
        private String userEmail;
        /**
         * 用户注册时间
         */
        private  String userRegTime;
        /**
         * 用户头像
         */
        private String userAvatar;
        /**
         * openid
         */
        private String openid;

        /**
         * 介绍
         */
        private String userIntroduce;
        /**
         * 背景图片
         */
        private String userBackground;

    }


}